<!--
 * @Author: 贾博林 11457230+jabolin@user.noreply.gitee.com
 * @Date: 2022-10-21 15:15:03
 * @LastEditors: 贾博林 11457230+jabolin@user.noreply.gitee.com
 * @LastEditTime: 2022-10-25 19:09:01
 * @FilePath: \v-shop\code\src\views\users.vue
 * @Description: users页面
-->
<template>
    <div>
        <!-- 面包屑 -->
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            <el-breadcrumb-item>用户列表</el-breadcrumb-item>
        </el-breadcrumb>
        <br>


        <!-- 卡片 -->
        <el-card>
            <el-input placeholder="请输入内容" v-model="params.query" style="width:428px">
                <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
            </el-input>

            <!-- 添加按钮 -->
            <el-button type="primary" class="tj" @click="add">添加用户</el-button>
            <br>
            <br>

            <!-- 表格 -->
            <el-table :data="users" border style="width: 100%">
                <el-table-column type="index" label="#">
                </el-table-column>
                <el-table-column prop="username" label="姓名">
                </el-table-column>
                <el-table-column prop="email" label="邮箱">
                </el-table-column>
                <el-table-column prop="mobile" label="电话">
                </el-table-column>
                <el-table-column prop="role_name" label="角色">
                </el-table-column>
                <el-table-column prop="mg_state" label="状态">
                    <!-- 插槽状态 -->
                    <template slot-scope="scope">
                        <el-switch v-model="scope.row.mg_state" active-color="#13ce66" inactive-color="#ff4949"
                            @change="zhangtai(scope.row)">
                        </el-switch>
                    </template>

                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit" size="mini"
                            @click="handleEdit(scope.$index, scope.row)"></el-button>
                        <el-button type="danger" icon="el-icon-delete" size="mini"
                            @click="handleDelete(scope.$index, scope.row)"></el-button>
                        <el-button type="warning" icon="el-icon-star-off" size="mini"
                            @click="handleEdit2(scope.$index, scope.row)"></el-button>
                    </template>
                </el-table-column>
            </el-table>


            <!-- 分页 -->
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="params.pagenum" :page-sizes="[1, 2, 3, 4]" :page-size="params.pagesize"
                layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </el-card>


        <!-- 添加模态框-->
        <el-dialog title="添加用户" :visible.sync="dialogFormVisible">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="ruleForm.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="ruleForm.password"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="ruleForm.email"></el-input>
                </el-form-item>
                <el-form-item label="手机" prop="mobile">
                    <el-input v-model="ruleForm.mobile"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
            </div>
        </el-dialog>


        <!-- 修改模态框-->
        <el-dialog title="修改用户" :visible.sync="dialogFormVisible2">
            <el-form :model="ruleForm2" :rules="rules2" ref="ruleForm2" label-width="100px">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="ruleForm2.username" disabled></el-input>
                </el-form-item>

                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="ruleForm2.email"></el-input>
                </el-form-item>
                <el-form-item label="手机" prop="mobile">
                    <el-input v-model="ruleForm2.mobile"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible2 = false">取 消</el-button>
                <el-button type="primary" @click="submitForm2('ruleForm2')">修改</el-button>
            </div>
        </el-dialog>

        <!-- 分配权限模态框-->
        <el-dialog title="分配权限" :visible.sync="dialogFormVisible3">
            <el-form :model="ruleForm3" :rules="rules3" ref="ruleForm3" label-width="100px">
                <p> 当前的用户：{{ ruleForm3.username }}</p>
                <p> 当前的角色：{{ ruleForm3.role_name }}</p>
                <el-form-item label="分配新角色" prop="region">
                    <el-select v-model="ruleForm.region" placeholder="请选择">
                        <el-option label="老板" value="laoban"></el-option>
                        <el-option label="经理" value="jingli"></el-option>
                        <el-option label="员工" value="yuangong"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible3 = false">取 消</el-button>
                <el-button type="primary" @click="submitForm3('ruleForm3')">修改权限</el-button>
            </div>
        </el-dialog>





    </div>
</template>

<script>
import { users, tjsj, xgzt, xgsj, scsj, fpqx } from '../http/users/api'
export default {
    name: 'users',
    data() {
        return {
            users: [],
            region: '',
            // 分页
            params: {
                pagenum: 1,
                pagesize: 4,
            },
            total: 0,
            keyword: '',
            dialogFormVisible: false,
            dialogFormVisible2: false,
            dialogFormVisible3: false,
            // 添加
            ruleForm: {
                username: '',
                password: "",
                email: "",
                mobile: ""
            },
            // 修改
            ruleForm2: {
                username: '',
                email: "",
                mobile: ""
            },
            // 分配权限
            ruleForm3: {

            },
            // 添加校验
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                ],
                email: [
                    { required: true, message: '请输入邮箱', trigger: 'blur' },
                    { type: 'email', message: '请输入正确的邮箱格式', trigger: ['blur', 'change'] },
                ],
                mobile: [
                    { required: true, message: '请输入电话', trigger: 'blur' },
                    { min: 11, max: 11, message: '长度在11个字符之间', trigger: 'blur' },
                ],
            },
            // 修改校验
            rules2: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                ],
                email: [
                    { required: true, message: '请输入邮箱', trigger: 'blur' },
                    { type: 'email', message: '请输入正确的邮箱格式', trigger: ['blur', 'change'] },
                ],
                mobile: [
                    { required: true, message: '请输入电话', trigger: 'blur' },
                    { min: 11, max: 11, message: '长度在11个字符之间', trigger: 'blur' },
                ],
            },
            // 分配校验
            rules3: {

            }
        };
    },

    components: {},

    computed: {},

    //钩子函数  
    created() {
        this.getdata()
    },

    // 方法
    methods: {
        // 渲染
        getdata() {
            users(this.params).then((res) => {
                this.users = res.data.data.users
                // 渲染分页
                this.total = res.data.data.total
                console.log(res);
            })
        },
        // 修改状态
        zhangtai(state) {
            xgzt(state).then(res => {
                // console.log(res,'xiugaizhuangtai');
                this.getdata()
                this.$message({
                    type: "success",
                    message: res.data.meta.msg
                })
            })
        },

        // 分页
        handleSizeChange(val) {
            this.params.pagesize = val
            this.getdata()
        },
        handleCurrentChange(val) {
            this.params.pagenum = val
            this.getdata()
        },

        // 点击出现添加模态框
        add() {
            this.dialogFormVisible = true
        },
        // 点击出现分配权限模态框
        handleEdit2(index, row) {
            console.log(index, row);
            this.dialogFormVisible3 = true
            this.ruleForm3 = row
        },

        // 点击出现修改模态框
        handleEdit(index, row) {
            console.log(index, row);
            this.dialogFormVisible2 = true
            this.ruleForm2 = row
        },
        //   删除
        handleDelete(index, row) {
            console.log(index, row);
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                scsj(row).then(res => {
                    // console.log(res,'shanchu');
                    this.getdata()
                    this.$message({
                        type: "success",
                        message: res.data.meta.msg
                    })
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },

        // 添加
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    tjsj(this.ruleForm).then(res => {
                        // console.log(res,'tianjia');
                        // 清空添加
                        this.ruleForm = {
                            username: '',
                            password: "",
                            email: "",
                            mobile: ""
                        },
                            this.$message({
                                type: "success",
                                message: res.data.meta.msg
                            })
                        this.dialogFormVisible = false
                    })
                } else {
                    this.$message({
                        type: "success",
                        message: res.data.meta.msg
                    })
                    return false;
                }
            });
        },

        // 修改
        submitForm2(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    xgsj(this.ruleForm2).then(res => {
                        console.log(res, 'xiugai');
                        this.$message({
                            type: "success",
                            message: res.data.meta.msg
                        })
                        this.dialogFormVisible2 = false
                    })
                } else {
                    this.$message({
                        type: "success",
                        message: res.data.meta.msg
                    })
                    return false;
                }
            });
        },

        // 分配权限
        submitForm3(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    fpqx("users/:id/role").then(res => {

                    })
                } else {
                    this.$message('更新角色成功')
                    return false;
                }
            });
        },

        // 搜索
        search() {
            this.params.pagenum = 1
            this.getdata()
        }
    },
    watch: {},
    mixins: []
}

</script>
<style  scoped>
.tj {
    margin-left: 20px;
}

p {
    margin-left: 20px;
}
</style>